#include "protreeitem.h"
#include"const.h"


ProTreeItem::ProTreeItem(QTreeWidget *view, const QString &name, const QString &path, int type)
    :QTreeWidgetItem(view, type), _name(name), _path(path), _root(this), _pre_item(nullptr), _next_item(nullptr)
{

}

ProTreeItem::ProTreeItem(QTreeWidgetItem *parent, const QString &name, const QString &path,
                         QTreeWidgetItem *root, int type)
    :QTreeWidgetItem(parent, type), _name(name), _path(path), _root(root), _pre_item(nullptr), _next_item(nullptr)
{

}

const QString &ProTreeItem::GetPath()
{
    return _path;
}

QTreeWidgetItem *ProTreeItem::GetRoot()
{
    return _root;
}

void ProTreeItem::SetPreItem(QTreeWidgetItem *item)
{
    _pre_item = item;
}

void ProTreeItem::SetNextItem(QTreeWidgetItem *item)
{
    _next_item = item;
}

ProTreeItem *ProTreeItem::GetPreItem()
{
    return dynamic_cast<ProTreeItem*>(_pre_item);
}

ProTreeItem *ProTreeItem::GetNextItem()
{
    return dynamic_cast<ProTreeItem*>(_next_item);
}

ProTreeItem *ProTreeItem::GetLastPicChild()
{
    if(this->type() == TreeItemPic) {
        return nullptr;
    }

    auto child_count = this->childCount();
    if(child_count == 0)
    {
        return nullptr;
    }

    for(int i = child_count - 1; i >= 0; i--)
    {
        auto* last_child = this->child(i);
        auto *last_tree_item = dynamic_cast<ProTreeItem*>(last_child);
        if(last_tree_item->type() == TreeItemPic) {
            return last_tree_item;
        }

        last_child = last_tree_item->GetLastPicChild();
        if(!last_child)
        {
            continue;
        }
        last_tree_item = dynamic_cast<ProTreeItem*>(last_child);
        return last_tree_item;
    }
    return nullptr;
}

ProTreeItem *ProTreeItem::GetFirPicChild()
{
    if(this->type() == TreeItemPic) {
        return nullptr;
    }

    auto child_count = this->childCount();
    if(child_count == 0)
    {
        return nullptr;
    }

    for(int i = 0; i < child_count; i++)
    {
        auto* first_child = this->child(i);
        auto *first_tree_item = dynamic_cast<ProTreeItem*>(first_child);
        if(first_tree_item->type() == TreeItemPic) {
            return first_tree_item;
        }

        first_child = first_tree_item->GetFirPicChild();
        if(!first_child)
        {
            continue;
        }
        first_tree_item = dynamic_cast<ProTreeItem*>(first_child);
        return first_tree_item;
    }
    return nullptr;
}

ProTreeItem *ProTreeItem::GetNextPicItem()
{
    if(_next_item && _next_item->type() == TreeItemPic)
    {
        return dynamic_cast<ProTreeItem*>(_next_item);
    }

    auto* parent = this->parent();
    if(!parent)
    {
        return nullptr;
    }

//    查询当前层级的当前元素的前几个元素
    int index = parent->indexOfChild(this);
    for(int i = index + 1; i < parent->childCount(); i++)
    {
        auto* next_item = dynamic_cast<ProTreeItem*>(parent->child(i));
        if(next_item->type() == TreeItemPic)
        {
            return next_item;
        }
        else if(next_item->type() == TreeItemDir)
        {
            auto* next_dir_first_item = next_item->GetFirPicChild();
            if(next_dir_first_item)
            {
                return next_dir_first_item;
            }
        }
    }
//    如果当前层级没有，那么从父级这个层级又开始找
    if(parent->type() == TreeItemPro)
    {
        return nullptr;
    }
    else
    {
        auto* pro_parent_item = dynamic_cast<ProTreeItem*>(parent);
        return pro_parent_item->GetNextPicItem();
    }
}

ProTreeItem *ProTreeItem::GetPrePicItem()
{
    if(_pre_item && _pre_item->type() == TreeItemPic)
    {
        return dynamic_cast<ProTreeItem*>(_pre_item);
    }

    auto* parent = this->parent();
    if(!parent)
    {
        return nullptr;
    }

//    查询当前层级的当前元素的前几个元素
    int index = parent->indexOfChild(this);
    for(int i = index - 1; i >= 0; i--)
    {
        auto* pre_item = dynamic_cast<ProTreeItem*>(parent->child(i));
        if(pre_item->type() == TreeItemPic)
        {
            return pre_item;
        }
        else if(pre_item->type() == TreeItemDir)
        {
            auto* pre_dir_last_item = pre_item->GetLastPicChild();
            if(pre_dir_last_item)
            {
                return pre_dir_last_item;
            }
        }
    }
//    如果当前层级没有，那么从父级这个层级又开始找
    if(parent->type() == TreeItemPro)
    {
        return nullptr;
    }
    else
    {
        auto* pro_parent_item = dynamic_cast<ProTreeItem*>(parent);
        return pro_parent_item->GetPrePicItem();
    }
}
